
class GamestateManager:
    def __init__(self, states):
        
        #a map of states so that states can be accessed via a string key
        self.states = states

        #the current state stack. All events will be passed to the state at the
        #top of the stack
        self.state_list = []
        self.state_list_e = []
        self.state_list_u = []
        self.state_list_d = []


    def push_state(self, data, state):
        """
        Pushes a new state onto the stack, making the new state the active state
        state: a string key referring to the state to be pushed
        """
        if len(self.state_list) > 0:
            self.state_list[-1].pause(data)

        self.states[state].begin(data)
        self.state_list.append(self.states[state])

    def pop_state(self, data):
        """
        Pops the current state from the stack, making the next state
        the active state
        """
        if len(self.state_list) == 0: return
        self.state_list[-1].die(data)
        self.state_list.pop()
        self.state_list[-1].resume(data)

    def current(self):
        return self.state_list[-1]

    def event(self, data, event, ms):
        self.state_list[-1].event(data, event, ms)

    def update(self, data, ms):
        self.state_list[-1].update(data, ms)
        return data.quit

    def draw(self, data):
        self.state_list[-1].draw(data)